using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace DIS_Client.MainManagers
{
	#region using
	using DevComponents.DotNetBar;
	using DIS_Client.Helpers;
	using System.Data;
	using System.Drawing;
	using System.IO;
	using DIS_Client.DIS_BUS_WS;
	#endregion
	public partial class FrmManageEmployee : Office2007Form
	{
		private DevComponents.DotNetBar.Controls.TextBoxX txtIDRole;
		private DevComponents.DotNetBar.Controls.TextBoxX txtIDRestaurant;

		public FrmManageEmployee(bool enableChon = false)
		{
			InitializeComponent();
		}

		private void FrmManageEmployee_Load(object sender, EventArgs e)
		{
			InitGridView();
			InitHiddenFields();
			LoadData();
            LoadRole();
			dtgvData.ClearSelection();
		}

        public void LoadRole()
        {
            if (CommonHelper.GetRole(GlobalVariables.g_CurrentUser) == ROLE.NATIONAL)
            {
                txtIDRole.Text = "2";
                txtRole.Text = "Local Manager";
                lblManageName.Text = "Local Manager";
                grpListData.Text = "List of Local Manager";
            }
            else if (CommonHelper.GetRole(GlobalVariables.g_CurrentUser) == ROLE.LOCAL)
            {
                txtIDRole.Text = "3";
                txtRole.Text = "Staff";
                lblManageName.Text = "Staff";
                grpListData.Text = "List of Staff";
                btnSelectRestaurant.Enabled = false;
                txtIDRestaurant.Text = GlobalVariables.g_CurrentUser.IDRestaurant.ToString();
                txtRestaurant.Text = GlobalVariables.g_CurrentUser.Restaurant.Name.ToString();
            }
        }

		private void btnAdd_Click(object sender, EventArgs e)
		{
			var errorText = "";
			if (!ValidateInput(EnumUpdateMode.INSERT, ref errorText))
			{
				MessageBoxEx.Show(errorText, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			if (ActionAdd())
			{
                MessageBoxEx.Show(CommonHelper.GetMessage("Insert", "Successful"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
				LoadData();
				btnReset_Click(sender, e);
			}
			else
			{
                MessageBoxEx.Show(CommonHelper.GetMessage("Insert", "Fail"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			var errorText = "";
			if (!ValidateInput(EnumUpdateMode.DELETE, ref errorText))
			{
				MessageBoxEx.Show(errorText, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
            if (MessageBoxEx.Show(CommonHelper.GetMessage("Delete", "Confirm"), "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
			{
				if (ActionDelete())
				{
                    MessageBoxEx.Show(CommonHelper.GetMessage("Delete", "Successful"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
					LoadData();
					btnReset_Click(sender, e);
				}
				else
				{
                    MessageBoxEx.Show(CommonHelper.GetMessage("Delete", "Fail"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

		private void btnUpdate_Click(object sender, EventArgs e)
		{
			var errorText = "";
			if (!ValidateInput(EnumUpdateMode.UPDATE, ref errorText))
			{
				MessageBoxEx.Show(errorText, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			if (ActionUpdate())
			{
                MessageBoxEx.Show(CommonHelper.GetMessage("Update", "Successful"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
				LoadData();
			}
			else
			{
                MessageBoxEx.Show(CommonHelper.GetMessage("Update", "Fail"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void btnExit_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void btnReset_Click(object sender, EventArgs e)
		{
			txtID.Text = "";
            if (GlobalVariables.g_CurrentUser.Role.Name.Trim() != "Local Manager")
            {
                txtIDRestaurant.Text = "";
                txtRestaurant.Text = "";
            }
			txtUserName.Text = "";
			txtFullName.Text = "";
		}

		private void dtgvData_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (dtgvData.SelectedRows == null || dtgvData.SelectedRows.Count == 0)
				return;
			var selectedItem = dtgvData.SelectedRows[0];
			txtID.Text = dtgvData.SelectedRows[0].Cells["ID"].Value.ToString();
			var item = BusService.Instance.Employee_SelectByID(int.Parse(txtID.Text));
			LoadDetailInfo(item);
		}

		public void LoadDetailInfo(EmployeeDTO item)
		{
			txtIDRole.Text = item.IDRole == -1 ? "" : item.IDRole.ToString();
			txtRole.Text = item.Role == null ? "" : item.Role.Name.ToString();
			txtIDRestaurant.Text = item.IDRestaurant == -1 ? "" : item.IDRestaurant.ToString();
			txtRestaurant.Text = item.Restaurant == null ? "" : item.Restaurant.Name.ToString();
			txtUserName.Text = item.UserName == null ? "" : item.UserName.ToString();
			txtFullName.Text = item.FullName == null ? "" : item.FullName.ToString();
		}

		private void InitGridView()
		{
			DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "ID",
				DataPropertyName = "ID",
				Name = "ID",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Role",
				DataPropertyName = "Role",
				Name = "Role",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Restaurant",
				DataPropertyName = "Restaurant",
				Name = "Restaurant",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "UserName",
				DataPropertyName = "UserName",
				Name = "UserName",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "FullName",
				DataPropertyName = "FullName",
				Name = "FullName",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5)) - 1
			};
			dtgvData.Columns.Add(objColumn);
		}

		public void InitHiddenFields()
		{
			txtIDRole = new DevComponents.DotNetBar.Controls.TextBoxX
			{
				Name = "txtIDRole",
				Text = ""
			};
			txtIDRole.Visible = false;
			txtIDRestaurant = new DevComponents.DotNetBar.Controls.TextBoxX
			{
				Name = "txtIDRestaurant",
				Text = ""
			};
			txtIDRestaurant.Visible = false;
		}

		private void LoadData()
		{
            var idRole = -1;
            if (CommonHelper.GetRole(GlobalVariables.g_CurrentUser) == ROLE.NATIONAL)
            {
                idRole = 2;
            }
            else if (CommonHelper.GetRole(GlobalVariables.g_CurrentUser) == ROLE.LOCAL)
            {
                idRole = 3;
            }

            var lstItem = BusService.Instance.Employee_SelectByIDRole(idRole);
			var dt = new DataTable();
			dt.Columns.Add("ID");
			dt.Columns.Add("Role");
			dt.Columns.Add("Restaurant");
			dt.Columns.Add("UserName");
			dt.Columns.Add("FullName");
			for (int i = 0; i < lstItem.Length; i++)
			{
                dt.Rows.Add(lstItem[i].ID, lstItem[i].IDRole == -1 ? "" : lstItem[i].Role.Name, lstItem[i].IDRestaurant == -1 ? "" : lstItem[i].Restaurant.Name, lstItem[i].UserName, lstItem[i].FullName);
			}
			DataView objTimeSheetDV = new System.Data.DataView(dt);
			objTimeSheetDV.AllowNew = false;
			dtgvData.AutoGenerateColumns = false;
			dtgvData.DataSource = objTimeSheetDV;
			dtgvData.ClearSelection();
		}

		private bool ValidateInput(EnumUpdateMode mode, ref string errorText)
		{
			if (mode == EnumUpdateMode.UPDATE || mode == EnumUpdateMode.DELETE)
			{
				if (txtID.Text == "")
				{
					errorText = "Please choose ID";
					return false;
				}
			}
			if (mode != EnumUpdateMode.DELETE)
			{
				if (txtIDRestaurant.Text == "")
				{
					 errorText = "Please select Restaurant";
					return false;
				}
				if (txtUserName.Text == "")
				{
					 errorText = "Please enter UserName";
					return false;
				}
			}
			return true;
		}

		private bool ActionAdd()
		{
			try
			{
				var item = new EmployeeDTO
				{
					IDRole = int.Parse(txtIDRole.Text),
					IDRestaurant = int.Parse(txtIDRestaurant.Text),
					UserName = txtUserName.Text,
					Password = Encryption.Encrypt("123456789"),
					FullName = txtFullName.Text,
				};
				if (!BusService.Instance.Employee_Insert(item))
				{
					return false;
				}
				return true;
			}
			catch
			{
				return false;
			}
		}

		private bool ActionUpdate()
		{
			try
			{
				var item = BusService.Instance.Employee_SelectByID(int.Parse(txtID.Text));
				item.IDRole = int.Parse(txtIDRole.Text);
				item.IDRestaurant = int.Parse(txtIDRestaurant.Text);
				item.UserName = txtUserName.Text;
				item.FullName = txtFullName.Text;
				return BusService.Instance.Employee_Update(item);
			}
			catch
			{
				return false;
			}
		}

		private bool ActionDelete()
		{
			try
			{
				return BusService.Instance.Employee_Delete(int.Parse(txtID.Text));
			}
			catch
			{
				return false;
			}
		}

		private void btnSelectRestaurant_Click(object sender, EventArgs e)
		{
			FrmManageRestaurant frm = new FrmManageRestaurant(true);
			frm.Handler += GetRestaurant;
			frm.ShowDialog();
		}

		public void GetRestaurant(object sender, EventArgs e)
		{
			var eventType = (TransferSimpleDataEvent)e;
			string[] comp = eventType.Data.Split(new char[] { '#' });
			txtIDRestaurant.Text = comp[0];
			txtRestaurant.Text = comp[1];
		}



	}
}